package com.blockmeta.bbs.immodule.adapter;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.LoadPicUtil;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.immodule.Constants;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.bean.beaninterface.ICircleEntry;
import com.blockmeta.bbs.businesslibrary.widget.CircleGradeAndKind;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.widget.CircleItemDecoration;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author AirThor
 * @date 2019/3/7.
 */
public class ContactsCircleAdapter extends BaseAdapter<ICircleEntry, BaseViewHolder> {

    private boolean editable = false;
    private boolean chooseSingle = false;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    private int mSelectedPos = -1;

    public ContactsCircleAdapter(int layoutResId, @Nullable List<ICircleEntry> data) {
        super(layoutResId, data);
    }


    public void setEditable(boolean editable, boolean chooseSingle) {
        this.editable = editable;
        this.chooseSingle = chooseSingle;
        mSelectedPos = -1;
        notifyDataSetChanged();
    }

    public List<ICircleEntry> getCheckedItems() {
        ArrayList<ICircleEntry> selectList = new ArrayList<>();
        int size = mSelectedPositions.size();
        for (int i = 0; i < size; i++) {
            if(mSelectedPositions.valueAt(i)){
                selectList.add(getItem(mSelectedPositions.keyAt(i)));
            }
        }
        return selectList;
    }

    public int getSelectedPos() {
        return mSelectedPos;
    }


    //设置给定位置条目的选择状态
    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }

    @Override
    public void bindToRecyclerView(RecyclerView recyclerView) {
        super.bindToRecyclerView(recyclerView);
        recyclerView.addItemDecoration(new CircleItemDecoration(this));
    }

    @Override
    protected void convert(BaseViewHolder helper, ICircleEntry item) {
        boolean isRecommend = item.recommendCircle();
        TextView tvJoinLimit = helper.getView(R.id.tv_join_limit);
        Button btnJoin = helper.getView(R.id.btn_join);
        tvJoinLimit.setVisibility(isRecommend? View.VISIBLE:View.GONE);
        btnJoin.setVisibility(isRecommend? View.VISIBLE:View.GONE);
        String btnStr = IMApplicationContext.getCircleDomainService().getJoinBtnStr(item);
        btnJoin.setText(btnStr);
        btnJoin.setEnabled(!item.getJoinState() && !Constants.LEVEL_NOT_ENOUGH.equals(btnStr));
        //为按钮绑定点击事件
        helper.addOnClickListener(R.id.btn_join);

        ImageView ivLogo = helper.getView(R.id.iv_avatar);
        LoadPicUtil.loadForIm(ivLogo,item.getCircleLogo());

        helper.setText(R.id.tv_circle_name,item.getCircleName());

        helper.setText(R.id.tv_circle_tag,item.getCircleSign());

        helper.setText(R.id.tv_circle_size,item.getCircleSize() + "人");

        helper.setText(R.id.tv_join_limit,item.getCircleJoinLimit());
        helper.setGone(R.id.tv_circle_ismy, item.getCreateById() == AccountHelper.getUidInt());

        CircleGradeAndKind circleGradeAndKind = helper.getView(R.id.cgk_info);
        circleGradeAndKind.setText(item.getCircleGrade(),item.getCircleKind());

        helper.setGone(R.id.iv_im_sign,item.isIdentification());

        ToggleButton toggleButton = helper.getView(R.id.tg_selector);
        toggleButton.setVisibility(editable? View.VISIBLE:View.GONE);
        toggleButton.setChecked(isItemChecked(helper.getClickPosition()));
        toggleButton.setClickable(false);
        toggleButton.setLongClickable(false);
    }


    public void checkChange(int position) {
        if(chooseSingle){
            if (mSelectedPos == position) {
                setItemChecked(position, false);
                mSelectedPos = -1;
                notifyItemChanged(position);
            } else if (mSelectedPos != position && mSelectedPos != -1) {
                //先取消上个item的勾选状态
                setItemChecked(mSelectedPos, false);
                notifyItemChanged(mSelectedPos);
                //设置新Item的勾选状态
                mSelectedPos = position;
                setItemChecked(mSelectedPos, true);
                notifyItemChanged(position);
            } else if (mSelectedPos == -1) {
                mSelectedPos = position;
                setItemChecked(mSelectedPos, true);
                notifyItemChanged(position);
            }
        }else {
            setItemChecked(position, !isItemChecked(position));
        }
    }

}
